define(['./PDFMatrix'], function(PDFMatrix) {
    'use strict';
    /**
     * enum for key of page data in JSON format.
     * @enum
     */
    var PageDataJsonFormat = {
        PAGE_WIDTH: "width",
        PAGE_HEIGHT: "height",
        PAGE_ROTATE: "rotate",
        PAGE_INDEX: "number",
        PAGE_MATRIX: "matrix"
    };


    function _init(pdfPage, jsonData) {
        try {
            pdfPage.pageIndex = jsonData[PageDataJsonFormat.PAGE_INDEX];
            pdfPage.pageWidth = jsonData[PageDataJsonFormat.PAGE_WIDTH];
            pdfPage.pageHeight = jsonData[PageDataJsonFormat.PAGE_HEIGHT];
            pdfPage.rotate = jsonData[PageDataJsonFormat.PAGE_ROTATE];
            pdfPage.pageMatrix = new PDFMatrix();
            var jsonMatrix = jsonData[PageDataJsonFormat.PAGE_MATRIX];
            if (typeof jsonMatrix != 'undefined' && jsonMatrix != null)
                pdfPage.pageMatrix.Set(jsonMatrix[0], jsonMatrix[1], jsonMatrix[2], jsonMatrix[3], jsonMatrix[4], jsonMatrix[5]);
            else
                pdfPage.pageMatrix.Set(1, 0, 0, 1, 0, 0);
        } catch (ex) {
            $.error(ex);
            return false;
        }
        return true;
    }


    /**
     * Create a PDF page object by data in JSON format.
     * @param {Object} pageJSONData
     * @constructor
     */
    var PDFPage = function(jsonData) {
        this.pageIndex = 0;
        this.pageWidth = 0;
        this.pageHeight = 0;
        this.rotate = 0;
        /**
         * Matrix for current page.
         * @type {WebPDF.PDFMatrix}
         * @private
         */
        this.pageMatrix = null;
        
        /**
         * Annotations for current page.
         * @type {{String : WebPDF.Data.PDFAnnotation}}
         * @private
         */        
        this.isModifyed = false;

        this.annotationsMap = {};
        this.annotJsonData = null;
        if (!_init(this, jsonData)) {
            $.error("Init PDFPage failed:" + jsonData);
        }
    };


    var fn = PDFPage.prototype;

        /**
         * Get index of page.
         * @returns {number} index of page
         */
    fn.getPageIndex = function() {
        return this.pageIndex;
    };
    /**
     * Get width of page in pt.
     * @returns {number} width of page in pt.
     */
    fn.getPageWidth = function() {
        return this.pageWidth;
    };
    /**
     * Get height of page in pt.
     * @returns {number} height of page in pt.
     */
    fn.getPageHeight = function() {
        return this.pageHeight;
    };
    /**
     * set width of page
     * @param {number} width
     */
    fn.setPageWidth = function(width) {
        this.pageWidth = width;
    };
    /**
     * set height of page
     * @param {number} height
     */
    fn.setPageHeight = function(height) {
        this.pageHeight = height;
    };
    /**
     * Get rotate of page.
     * @returns {number} rotate of page.
     */
    fn.getPageRotate = function() {
        return this.rotate;
    };
    /**
     * Set rotate of page.
     * @param {number} rotate
     */
    fn.setPageRotate = function(rotate) {
        this.rotate = rotate;
    };

    /**
     * Return matrix of page.
     * @returns {WebPDF.PDFMatrix}
     */
    fn.getPageMatrix = function() {
        return this.pageMatrix;
    };

    //extra functions for Image Engine

    /**
     * Get annotation in map.
     * @param annotName
     * @param annotMap
     * @returns {*}
     * @private
     */
    function _getAnnotInMap(annotName, annotMap) {
            if (!annotMap) return null;
            return annotMap[annotName];
    }

    fn.getAnnotByName = function(annotName) {
            if (!this.annotationsMap) return null;
            return this.getAnnotInMap(annotName, this.annotationsMap);
        };

    fn.enumAnnots = function(callback) {
        for (var key in this.annotationsMap) {
            if (callback.call(this, this.annotationsMap[key]) === false) {
                break;
            }
        }
    };

    /**
     * 批量删除annotation
     * @param  {Object} annotMapToDelete 需要删除的annotation列表，格式为 {annotName : 1}
     * @private
     */
    fn.deleteAnnots = function(annotMapToDelete) {
        for (var j = 0; j < this.annotJsonData.length; j++) {
            var jsonAnnot = this.annotJsonData[j];
            var name = jsonAnnot[PageDataJsonFormat.name];
            if (annotMapToDelete[name]) {
                this.annotJsonData.splice(j, 1);
                j = j - 1;
                delete this.annotationsMap[name];
            }
        }
    };


    fn.addAnnot = function(annot) {
        var annName = annot.getAnnotName;
        return this.annotationsMap[annName] = annot;
    };

    fn.getAnnotCount = function() {
        if (!this.annotationsMap) return 0;
        return this.annotationsMap.size();
    };

    fn.getAnnotJsonData = function() {
        return this.annotJsonData;
    };

    fn.setAnnotJsonData = function(annotJsonData) {
        this.annotJsonData = annotJsonData;
    };


    fn.isModified = function() {
        return this.isModifyed;
    };

    fn.setModified = function(isModifyed) {
        this.isModifyed = isModifyed;
    };    

    return PDFPage;
});